package com.zkingsoft.actions.common.upload;




import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.json.simple.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.actions.common.BaseController;
import com.zkingsoft.constance.Dictionary;
import com.zkingsoft.pojo.AjaxResult;

/**
 * @description 文件上传action
 * @author 罗凯
 * @date 2016-11-01
 */
@Controller
@RequestMapping(value = "admin/upload")
public class UploadFileController extends BaseController {
	
	@RequestMapping(value = "/uploadFile")
	public  @ResponseBody AjaxResult upLoadFile(HttpServletResponse response,HttpServletRequest request) throws IOException, FileUploadException{
		System.out.println("上传文件");
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter out=response.getWriter();
		
		String flag = request.getParameter("flag");
		String hiddenImg = request.getParameter("hiddenImg");
		//回调函数
		String callBack=request.getParameter("callBack");
		if(callBack!=null){
			callBack=callBack+"();";
		}
		UploadUtils utils=new UploadUtils();
		
		UploadResult result=utils.uploadFile(request);
		System.out.println(result);
		if(UploadResult.STATUS_OK.equals(result.getStatus())){
			out.print(
					"<script type='text/javascript'>"+
					"		window.parent.document.getElementById('"+flag+"').setAttribute('src', '"+result.getUrl()+"');"+
					"		window.parent.document.getElementById('"+hiddenImg+"').value='"+result.getUrl()+"';"+callBack+
					"</script>");
		}else{
			out.print(
					"<script type='text/javascript'>"+
					"		window.parent.alert('"+result.info+"');"+
					"</script>");
		}
		
		return null;
	}
}